<!-- 司机管理 -->
<template>
  <div class="dashboard-container driver customer-list-box">
    <el-card
      class="search-card"
      shadow="never"
    >
      <el-form
        ref="searchForm"
        :model="params"
        label-width="80px"
      >
        <div class="filter-container">
          <el-row :gutter="60">
            <el-col :span="8">
              <el-form-item
                label="司机账号:"
                style="margin-bottom: 0px"
                label-width="110px"
              >
                <el-input
                  v-model="params.account"
                  placeholder="请输入司机账号"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="司机姓名:"
                style="margin-bottom: 0px"
                label-width="110px"
              >
                <el-input
                  v-model="params.name"
                  placeholder="请输入司机姓名"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="司机手机号:"
                style="margin-bottom: 20px"
                label-width="110px"
              >
                <el-input
                  v-model="params.phone"
                  placeholder="请输入司机手机号"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="所属机构:"
                style="margin-bottom: 20px"
                label-width="110px"
              >
                <treeselect
                  v-model="params.agentId"
                  :options="agencyOptions"
                  :normalizer="normalizer"
                  :searchable="true"
                  style="width: 100%"
                  placeholder="请选择所属机构"
                  no-options-text="暂无数据"
                  no-results-text="暂无数据"
                  @select="selectEndAgency"
                  @open="open"
                  @close="close"
                />
                <img
                  ref="arrows"
                  src="@/assets/pullDown.png"
                  style="
                      position: absolute;
                      width: 18px;
                      height: 18px;
                      right: 9px;
                      top: 12px;
                      cursor: pointer;
                      pointer-events: none;
                      transition-duration: 0.3s;
                    "
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-button
                type="warning"
                @click="handleFilter"
              >搜索</el-button>
              <el-button
                class="reset-btn"
                plain
                @click="resetForm"
              >重置</el-button>
            </el-col>

          </el-row></div>
      </el-form>
    </el-card>
    <el-card
      class="box-card"
      shadow="never"
    >
      <el-table
        class="waybill-table"
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column
          type="index"
          align="center"
          label="序号"
          width="80"
        >
        </el-table-column>
        <el-table-column
          prop="account"
          align="center"
          label="司机账号"
        >
        </el-table-column>
        <el-table-column
          prop="name"
          align="center"
          label="司机姓名"
        >
        </el-table-column>
        <el-table-column
          prop="mobile"
          align="center"
          label="司机电话"
        >
        </el-table-column>
        <el-table-column
          prop="truck.licensePlate"
          align="center"
          label="关联车辆"
        >
        </el-table-column>
        <el-table-column
          prop="agency.name"
          align="center"
          label="所属机构"
        >
        </el-table-column>
        <el-table-column
          prop="workStatus"
          :formatter="workStatusFormatter"
          align="center"
          label="工作状态"
        >
        </el-table-column>
        <el-table-column
          label="操作"
          width="170"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              class="edit"
              type="text"
              size="small"
              @click="getDetail(scope.row.userId)"
            >查看</el-button>
            <span class="Partingline">|</span>
            <el-button
              class="edit"
              type="text"
              size="small"
              @click="assignedCar(scope.row)"
            >配置车辆</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="params.page"
        :page-sizes="[5,10, 15, 20, 25]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      v-if="dialogVisible"
      title="配置车辆"
      :visible.sync="dialogVisible"
      width="640px"
    >
      <CarConfig
        :dialog-visible="dialogVisible"
        :car-driver-info="carDriverInfo"
        @changeIsShowDialog="changeIsShowDialog"
      ></CarConfig>
    </el-dialog>
  </div>
</template>
<script>
import { getDriver } from '@/api/driver'
import CarConfig from './components/car-config.vue'
import { agencyList } from '@/api/institutions'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'Courses',
  components: {
    CarConfig,
    Treeselect
  },
  data() {
    return {
      dialogVisible: false,
      agencyOptions: [],
      tableData: [],
      carDriverInfo: [],
      total: 0,
      params: {
        page: 1,
        pageSize: 5,
        account: '', // 司机账号
        name: '', // 司机姓名
        phone: '', // 司机电话
        agentId: null// 机构ID
      },
      agrntList: [],
      normalizer(node) {
        return {
          id: node.id,
          label: node.name,
          children: node.children
        }
      }
    }
  },
  created() {
    this.getDriverList()
    this.getAgencyList()
  },
  methods: {
    // 获取地机构列表数据
    async getAgencyList() {
      const { data: res } = await agencyList()
      // 获取数据给options
      this.agencyOptions = JSON.parse(res)
    },
    selectEndAgency(val) {
      this.params.agentId = val.id
    },
    open() {
      this.$refs.arrows.style.transform = 'rotate(-180deg)'
    },
    close() {
      this.$refs.arrows.style.transform = 'rotate(0deg)'
    },

    changeIsShowDialog(val) {
      this.dialogVisible = val
    },
    assignedCar(row) {
      const that = this
      that.carDriverInfo = row
      const h = this.$createElement
      this.$msgbox({
        title: `配置车辆`,
        message: h('p', null, [
          h('p', null, `配置车辆需要满足以下条件：`),
          h('span', { style: 'margin-right: 32px' }, `1、司机信息已完善`),
          h('span', null, `2、司机已设置排班`),
          h('p', null, `3、司机绑定车辆未在启动状态`)
        ]),
        showCancelButton: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(async (action) => {
        that.dialogVisible = true
      }).catch(() => { })
    },
    getDetail(id) {
      this.$router.push(`driver-detail?id=${id}`)
    },
    workStatusFormatter(row) {
      const MAP = {
        0: '休息',
        1: '上班'
      }
      return MAP[row.workStatus]
    },
    async handleFilter() {
      await this.getDriverList()
    },
    async resetForm() {
      await this.$refs.searchForm.resetFields()
      this.params.page = 1
      this.params.pageSize = 10
      this.params.account = ''
      this.params.name = ''
      this.params.phone = ''
      this.params.agentId = null
      await this.getDriverList()
    },
    async handleSizeChange(val) {
      this.params.pageSize = val
      this.getDriverList()
      await this.getDriverList()
    },
    async handleCurrentChange(val) {
      this.params.page = val
      await this.getDriverList()
    },
    async getDriverList() {
      const res = await getDriver(this.params)
      this.tableData = res.data.items
      console.log(this.tableData)
      this.total = Number(res.data.counts)
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.closeImg{
  width: 18px;
}
::v-deep .el-form-item__content{
  width: 240px;
}

::v-deep .vue-treeselect__menu-container {
  z-index: 9999 !important;
}
.el-pagination{
  padding: 20px 0;
  position: relative;
  left: 30%;
}
.search-card{
  margin: 8px 20px;
  padding: 20px 30px 0 30px;
  ::v-deep .el-card__body{
    padding: 0;
    .el-form-item__label{
      font-weight: 400;
    }
    .el-input__inner{
        width: 240px;
      }
  }
}
.cell{
    text-align: left;
    .edit{
      color: #419EFF;
    }
    .delete{
      color: #F56C6C;
    }
    .Partingline{
      color: #E3E3E3 ;
    }
}
.box-card{
  margin:  20px;
  padding: 30px;
  ::v-deep .el-card__body{
    padding: 0;
  }
}
.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 40px;
}
// 下拉框
::v-deep .el-select {
  width: 100%;
}

::v-deep .el-card{
  overflow: initial;
}
</style>

